import Message from "element-ui/packages/message/src/main";
import * as graphAPI from "../../api/graphAPI";

const graphStorage = {
    state: {},
    mutations: {},
    actions: {
        getGraphDetail: async ({commit}, graphId) => {
            let res = await graphAPI.getGraphDetailAPI(graphId);
            if (!res) {
                Message({
                    message: "获取图谱信息失败",
                    type: "error"
                });
            }
            return res;
        },
        getAllGraph: async ({commit}) => {
            let res = await graphAPI.getAllGraphAPI();
            return res;
        },
        search: async ({commit}, data) => {
            let res = await graphAPI.searchAPI(data);
            return res;
        },
        getGraphById: async ({commit}, graphId) => {
            let res = await graphAPI.getGraphByIdAPI(graphId);
            let message = "";
            if (!res) {
                message = "没有找到对应的图谱";
            }
            return {
                graph: res.data,
                message: message
            };
        },
        getGraphByName: async ({commit}, graphName) => {
            let res = await graphAPI.getGraphByNameAPI(graphName);
            let message = "";
            if (!res) {
                message = "没有找到对应的图谱";
            }
            return {
                graph: res,
                message: message
            };
        },
        addGraph: async ({commit}, graphName) => {
            let res = await graphAPI.addGraphAPI(graphName);
            if (res.code !== 200) {
                Message({
                    message: "创建失败",
                    type: "error"
                });
            } else {
                Message({
                    message: "创建成功",
                    type: "success"
                });
            }
            return res.data;
        },
        deleteGraph: async ({commit}, graphId) => {
            let res = await graphAPI.deleteGraphAPI(graphId);
            if (res.code === 200) {
                Message({
                    message: "删除成功",
                    type: "success"
                });
            } else {
                Message({
                    message: "删除失败",
                    type: "error"
                });
            }
        },
        exportGraphAsXml: async ({commit}, graphId) => {
            function downloadFile(content, filename) {
                let a = document.createElement("a");
                let blob = new Blob([content]);
                let url = window.URL.createObjectURL(blob);
                a.href = url;
                a.download = filename;
                a.click();
                window.URL.revokeObjectURL(url);
            }

            let res = await graphAPI.exportGraphAsXmlAPI(graphId);
            if (res) {
                downloadFile(res, "graph" + graphId + ".xml");
                Message({
                    message: "导出成功",
                    type: "success",
                });
            } else {
                Message({
                    message: "导出失败",
                    type: "error",
                });
            }
        },
        importGraph: async ({commit}, data) => {
            let res = await graphAPI.importGraphAPI({id: data.id, file: data.file});
            if (res) {
                Message({
                    message: "导入成功",
                    type: "success"
                });
            } else {
                Message({
                    message: "导入失败",
                    type: "error"
                });
            }
        },
        updateGraph: async ({commit}, graph) => {
            let res = await graphAPI.updateGraphAPI(graph);
            if (res) {
                Message({
                    message: "更新成功",
                    type: "success"
                });
            } else {
                Message({
                    message: "更新失败",
                    type: "error"
                });
            }
        }
    }

};

export default graphStorage;